using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using Spacejock;
using System;
using System.Reflection;
using System.Windows.Forms;
using yW5Lib;

namespace yWriter6
{
	public class clsImportWorkInProgress
	{
		private string HelpText;

		public clsImportWorkInProgress()
		{
			HelpText = "A work in progress is an HTML, RTF or TXT file with the following delimiters:\r\nThe file should contain chapter headings such as 'Chapter 1', 'Chapter 2' and so on.\r\nOptional: if there are multiple scenes per chapter, they should be separated with three asterisks on a blank line: * * *";
		}

		public bool ImportWorkInProgress(ref clsProject cProject, bool V6File)
		{
			bool result = false;
			frmImportWIP frmImportWIP = new frmImportWIP();
			frmImportWIP.ShowDialog();
			if (!frmImportWIP.Cancelled)
			{
				clsRTF clsRTF = new clsRTF(ref clsShared.cLog, ref clsShared.cSettings);
				string TextString = "";
				if (!clsShared.cFile.ReadTextFile(frmImportWIP.txtWipFile.Text, ref TextString))
				{
					Interaction.MsgBox("Unable to read " + frmImportWIP.txtWipFile.Text + "\r\nPlease ensure the file is not open in another program.");
				}
				else if (cProject.CreateEmptyProject(V6File))
				{
					RichTextBox fRTF = new RichTextBox();
					RichTextBox richTextBox = new RichTextBox();
					RichTextBox richTextBox2 = new RichTextBox();
					try
					{
						if (Operators.CompareString(Strings.LCase(clsShared.cFile.ExtOnly(frmImportWIP.txtWipFile.Text)), "rtf", TextCompare: false) != 0)
						{
							if (Operators.CompareString(Strings.LCase(clsShared.cFile.ExtOnly(frmImportWIP.txtWipFile.Text)), "txt", TextCompare: false) == 0)
							{
								if (((Strings.InStr(TextString, "<") > 0) & (Strings.InStr(TextString, ">") > 0)) && clsShared.cLang.TransMsgBox("Your import file is plain text, but it also contains possible html codes. Do you want to strip out any html? (This will delete everything between < and > markers.)", MsgBoxStyle.YesNo) == MsgBoxResult.Yes)
								{
									clsShared.cHtml.HTMLToPlainText(ref TextString);
								}
							}
							else
							{
								clsShared.cHtml.HTMLToPlainText(ref TextString);
							}
						}
						try
						{
							fRTF.Rtf = TextString;
						}
						catch (Exception ex)
						{
							ProjectData.SetProjectError(ex);
							Exception ex2 = ex;
							fRTF.Text = TextString;
							ProjectData.ClearProjectError();
						}
						int num = 0;
						if (!((Strings.InStr(fRTF.Text, "Chapter ") == 0) & (Strings.InStr(fRTF.Text, "CHAPTER ") == 0) & (Strings.InStr(fRTF.Text, clsShared.cLang.GetTranslation("Chapter") + " ") == 0)))
						{
							clsCharacter cChr = new clsCharacter();
							cChr.Title = "Unnamed";
							int num2 = 0;
							cProject.AddChar(ref cChr);
							do
							{
								int num3 = Conversions.ToInteger(FindClosestChapter(ref fRTF, num2));
								clsChapter cChapter = new clsChapter();
								num++;
								if (num == 1)
								{
									cChapter.Title = clsShared.cLang.GetTranslation("Prologue");
									cChapter.DescText = clsShared.cLang.GetTranslation("This 'chapter' contains everything before the first 'Chapter X' heading");
								}
								else
								{
									cChapter.Title = clsShared.cLang.GetTranslation("Chapter", " ") + Conversions.ToString(num - 1);
									cChapter.DescText = "";
								}
								cProject.AddChapter(ref cChapter);
								int iD = cChapter.ID;
								if (num2 == 0)
								{
									fRTF.Select(num2, num3 - num2 - 1);
								}
								else
								{
									fRTF.Select(num2 - 1, num3 - num2 - 1);
								}
								richTextBox.Rtf = fRTF.SelectedRtf;
								int num4;
								clsScene Scene;
								do
								{
									num4 = Strings.InStrRev(richTextBox.Text, "* * *");
									if (num4 > 0)
									{
										richTextBox.Select(num4 - 1, richTextBox.Text.Length - num4 + 1);
										richTextBox2.Rtf = richTextBox.SelectedRtf;
										richTextBox.SelectedText = "";
										richTextBox2.Select(0, 5);
										richTextBox2.SelectedText = "";
										Scene = new clsScene();
										Scene.V6 = cProject.V6File;
										Scene.SetMarkup(clsRTF.RTFToMarkup(richTextBox2.Rtf));
										Scene.Title = "Scene";
										Scene.DescText = "";
										Scene.Goal = "";
										Scene.Conflict = "";
										Scene.Outcome = "";
										Scene.SetViewpointByID(1);
										cProject.AddSceneToProjectAndChapter(ref Scene, iD, 1);
									}
								}
								while (num4 != 0);
								Scene = new clsScene();
								Scene.V6 = cProject.V6File;
								if (Strings.InStr(richTextBox.Text, "\r\n") > 0)
								{
									richTextBox.Select(0, Strings.InStr(richTextBox.Text, "\r\n"));
								}
								else
								{
									richTextBox.Select(0, Strings.InStr(richTextBox.Text, "\n"));
								}
								richTextBox.SelectedText = "";
								Scene.SetMarkup(clsRTF.RTFToMarkup(richTextBox.Rtf));
								Scene.Title = "Scene";
								Scene.SetViewpointByID(1);
								cProject.AddSceneToProjectAndChapter(ref Scene, iD, 1);
								cChapter = cProject.get_ChapterByID(iD);
								int sceneCount = cChapter.SceneCount;
								for (int i = 1; i <= sceneCount; i++)
								{
									cProject.get_SceneByID(cChapter.get_SceneIDByIndex(i)).Title = "Scene " + Conversions.ToString(i);
								}
								num2 = Conversions.ToInteger(FindClosestChapter(ref fRTF, num2 + 1));
							}
							while (num2 != fRTF.Text.Length);
							if (!cProject.CriticalSave())
							{
								return result;
							}
							result = true;
							return result;
						}
						clsShared.cLang.TransMsgBox("I can't see any chapter headings in your file. Please read the following:@1@@2@", MsgBoxStyle.Exclamation, "", "\r\n", HelpText);
						return result;
					}
					catch (Exception ex3)
					{
						ProjectData.SetProjectError(ex3);
						Exception ex4 = ex3;
						clsShared.cLog.AddAction(ex4, MethodBase.GetCurrentMethod());
						ProjectData.ClearProjectError();
						return result;
					}
				}
			}
			return result;
		}

		private object FindClosestChapter(ref RichTextBox fRTF, int ChapLoc)
		{
			int num = Strings.InStr(ChapLoc + 1, fRTF.Text, "Chapter ");
			int num2 = Strings.InStr(ChapLoc + 1, fRTF.Text, "CHAPTER ");
			int num3 = Strings.InStr(ChapLoc + 1, fRTF.Text, clsShared.cLang.GetTranslation("Chapter") + " ");
			if (num2 != 0 && ((num == 0) | (num2 < num)))
			{
				num = num2;
			}
			if (num3 != 0 && ((num == 0) | (num3 < num)))
			{
				num = num3;
			}
			if (num == 0)
			{
				num = Strings.Len(fRTF.Text);
			}
			return num;
		}
	}
}
